package com.ding.dailyup.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ding.dailyup.annotation.LoginUser;
import com.ding.dailyup.emum.StrategyStatusEnum;
import com.ding.dailyup.entity.response.StockStrategyResp;
import com.ding.dailyup.model.UserBuyStrategy;
import com.ding.dailyup.service.StockStrategyService;
import com.ding.dailyup.entity.JsonResult;
import com.ding.dailyup.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/strategy")
@Api(tags = "策略模块")
public class StrategyController {
	@Autowired
	private StockStrategyService stockStrategyService;

	@Autowired
	private UserService userService;

	@ApiOperation(value = "策略列表")
	@GetMapping(value = "/list")
	public JsonResult<List<StockStrategyResp>> strategyList(
			@LoginUser Long userId) {
		List<StockStrategyResp> strategyList = stockStrategyService
				.listStrategies().parallelStream().map(stockStrategy -> {
					StockStrategyResp resp = new StockStrategyResp();
					BeanUtil.copyProperties(stockStrategy, resp);
					return resp;
				}).collect(Collectors.toList());

		List<Long> buyStrategy = userService.getUserBuyStrategy(userId).stream()
				.map(UserBuyStrategy::getStrategyId)
				.collect(Collectors.toList());

		strategyList.forEach(item -> {
			Long id = item.getId();
			if (buyStrategy.contains(id)) {
				item.setStatus(StrategyStatusEnum.FREE.status());
			}
		});
		return JsonResult.succResult(strategyList);
	}

	@ApiOperation(value = "策略详情")
	@GetMapping(value = "/detail")
	public JsonResult strategyDetail(
			@RequestParam(value = "strategyId") Long strategyId) {

		return JsonResult.succResult();
	}
}
